#!/usr/bin/perl

use strict;
use warnings;

$| = 1;

my $default_port = 9307;
my $port = shift || $default_port;

if ($port && ($port !~ /^\d+$/s || $port <= 1024 || $port > 9999)) {
  die "setup.pl: port parameter must be an integer between 1024 and 10000\n";
}

use LWP::UserAgent;

my ($version,
    $apache_file,
    $apache_folder,
    $apache_root,
);

my $tmp_folder = "testing/tmp";

GET_LATEST_VERSION: {
  print "-- checking for latest apache version...";
  my $ua = LWP::UserAgent->new;
  $ua->timeout(10);
  $ua->env_proxy;
  my $url = 'http://www.apache.org/dist/httpd/';
  my $response = $ua->get($url);
  if ($response->is_success) {
    ($version) = $response->content =~ m/CURRENT\-IS\-(\d+\.\d+\.\d+)/
      or die "could not get version from content at $url";
  }
  else {
    die "could not get lastest version from $url - " . $response->status_line;
  }
  print " latest version is $version\n\n";
}

DOWNLOAD_APACHE: {
  my $url = "http://www.apache.org/dist/httpd/httpd-$version.tar.gz";
  ($apache_file) = $url =~ /([^\/]+)$/;
  mkdir $tmp_folder or die "ould not make directory $tmp_folder: $!"
    unless -e $tmp_folder;
  if (-e "$tmp_folder/$apache_file") {
    print "  file $tmp_folder/$apache_file exists, using that.\n\n";
  }
  else {
    print "-- downloading $url to $tmp_folder/...\n\n";
    system("wget -P $tmp_folder/ $url") == 0
      or die "wget was not successful: $!";
    print "   download complete.\n\n";
  }
  ($apache_folder) = $apache_file =~ m/^(.+)\.tar\.gz$/;
  print "-- removing any old apache build dir...";
  system("rm -rf $tmp_folder/$apache_folder") == 0
    or die "could not remove $tmp_folder/$apache_folder, $!";
  print "done.\n\n";
  print "-- unpacking apache...";
  system("cd $tmp_folder && tar xzf $apache_file") == 0
    or die "could not unpack apache";
  print "done.\n\n";
}

COMPILE_APACHE: {
  print "-- compining apache...\n\n";
  $apache_root = `pwd`;
  chomp $apache_root;
  $apache_root .= '/testing/apache';
  system("cd $tmp_folder/$apache_folder && ./configure --prefix=$apache_root && make && make install") == 0
    or die "could not compile apache, $!";
  print "\n    apache compiled.\n\n";
}

REMOVE_TEMPORARY_DIRECTORY: {
  print "-- removing temporary directory...";
  system("rm -rf $tmp_folder") == 0
    or die "could not remove $tmp_folder, $!";
  print "done.\n\n";
}

REPLACE_PORT: {
  for my $conf ("$apache_root/conf/httpd.conf", "$apache_root/conf/original/httpd.conf") {
    my $contents = slurp($conf);
    $contents =~ s/Listen 80/Listen 127.0.0.1:$port/g;
    slop($conf, $contents);
  }

  if ($port != $default_port) {
    print "Run EXPORT TEST_PORT=$port before running tests\n";
  }
}

################# utility #################

sub slurp {
  my $file = shift;
  local $/ = undef;
  open my $fh, '<', $file or die "cannot slurp $file, $!";
  my $contents = <$fh>;
  close $fh;
  $contents;
}

sub slop {
  my ($file, $contents) = @_;
  open my $fh, '>', $file or die "cannot slop $file, $!";
  print $fh $contents;
  close $fh;
}

